home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0187.ZIP / GUESSIT.PAS < prev    next >
Pascal/Delphi Source File  |  1985-01-20  |  2KB  |  96 lines

  1. (*
  2.  *  GUESS IT
  3.  *  A computer guessing game
  4.  *)
  5. program guessit (input,output);
  6. const
  7.    maxtrys        =  6;        (* user allowed 6 tries to guess number *)
  8.    maxnum         = 100;       (* maximum number *)
  9.    minnum         =  1;        (* minimum number *)
  10.  
  11. var
  12.    ch : char;               (* used to answer questions *)
  13.    guess : integer;         (* human's guess *)
  14.    number: minnum..maxnum;  (* the number itself *)
  15.    numtry: 0..maxtrys;      (* number od times human has guessed *)
  16.  
  17.  
  18.   (*
  19.    * instructions
  20.    * prints out the instructions
  21.    *)
  22.   procedure instructions;
  23.   begin
  24.      writeln('This is match it');
  25.      writeln;
  26.      writeln('I will choose a number between 1 and 100.');
  27.      writeln('You will try to guess that number.');
  28.      writeln('If you guess wrong, I will tell you if you guessed');
  29.      writeln('too high, or too low.');
  30.      writeln('you have ',maxtrys,' tries to get the number.');
  31.      writeln;
  32.      writeln('Enjoy');
  33.      writeln;
  34.   end;   (* instructions *)
  35.  
  36.  
  37.   (*
  38.    * getnum
  39.    * get a guess from the human,
  40.    * with error checking
  41.    *)
  42.   procedure getnum;
  43.   begin
  44.      write('Your guess? ');
  45.  
  46.      (*
  47.       * wait for a character
  48.       *)
  49.  
  50.      begin
  51.  
  52.         readln(guess);
  53.         if guess > maxnum then
  54.         begin
  55.            writeln('Illegal number');
  56.            getnum
  57.         end
  58.         else
  59.         begin
  60.            if guess > number then
  61.               writeln('too high')
  62.            else
  63.               if guess < number then
  64.                  writeln('too low')
  65.               else
  66.                  writeln('correct!!!')
  67.         end
  68.      end
  69.   end;   (* getnum *)
  70.  
  71. (*
  72.  * main
  73.  *)
  74.  
  75. begin
  76.    instructions;
  77.    randomize;
  78.    while pos(ch,'nN')=0 do
  79.    begin
  80.       guess := 0;
  81.       numtry := 0;
  82.       number := random(maxnum)+1-minnum;   (* get number *)
  83.       while (numtry < maxtrys) and (guess <> number) do
  84.       begin
  85.          getnum;
  86.          numtry := numtry + 1
  87.       end;
  88.       writeln;
  89.       if guess <> number then
  90.          writeln('The number was ',number);
  91.       writeln;
  92.       write('want to try again? ');
  93.       readln(ch)
  94.    end
  95. end.
  96.